package com.tguigu.study.config;

import com.tguigu.study.service.FunctionAssistant;
import com.tguigu.study.service.InvoiceHandler;
import dev.langchain4j.agent.tool.ToolSpecification;
import dev.langchain4j.model.chat.ChatModel;
import dev.langchain4j.model.chat.request.json.JsonObjectSchema;
import dev.langchain4j.model.openai.OpenAiChatModel;
import dev.langchain4j.service.AiServices;
import dev.langchain4j.service.tool.ToolExecutor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.Map;

/**
 * @author Marco
 * @Description
 * @date 2025/8/21 19:13
 * @email 3293336923@qq.com
 */
@Configuration
public class LLMConfig {

    @Bean
    public ChatModel chatModel() {
        return
                OpenAiChatModel.builder()
                        .apiKey(System.getenv("ALIQW"))
                        .modelName("qwen-plus")
                        .baseUrl("https://dashscope.aliyuncs.com/compatible-mode/v1")
                        .build();
    }


//     https://docs.langchain4j.dev/tutorials/tools#low-level-tool-api


//    第一组：  低阶 api 的调用
    @Bean(name = "lowFunctionAssistant")
    public FunctionAssistant   FunctionAssistant(ChatModel  chatModel) {

//          ToolSpecification 工具说明类
        ToolSpecification toolSpecification = ToolSpecification.builder()
                .name("开具发票助手")
                .description("根据用户提交的开票信息，开具发票")
                .parameters(JsonObjectSchema.builder()
                        .addStringProperty("companyName", "公司名称")
                        .addStringProperty("dutyNumber", "税号序列")
                        .addStringProperty("amount", "开票金额，保留两位有效数字")
                    .build())
            .build();

//        业务逻辑
        ToolExecutor  toolExecutor = ((toolExecutionRequest, memoryId) -> {
            System.out.println(toolExecutionRequest.id());
            System.out.println(toolExecutionRequest.name());
            String arguments = toolExecutionRequest.arguments();
            System.out.println("arguments1***》"+arguments);
            return  "开具成功！";
        });

        return AiServices.builder(FunctionAssistant.class)
                .chatModel(chatModel)
                .tools(Map.of(toolSpecification , toolExecutor ))
                .build();
    }




//    第二组：  高阶 api 的调用

    @Bean("highFunctionAssistant")
    public      FunctionAssistant   functionAssistant(ChatModel  chatModel){
        return
                AiServices.builder(FunctionAssistant.class)
                        .chatModel(chatModel)
                        .tools(new InvoiceHandler())
                        .build();
    }


}
